#include <queue>
#include <utility>
#include <vector>
using namespace std;

class Solution
{
    int m{}, n{};
    bool visit[51][51]{};
    int dx[4] = {-1, 1, 0, 0};
    int dy[4] = {0, 0, -1, 1};
public:
    int maxAreaOfIsland(vector<vector<int>>& grid)
    {
        int ret = 0;
        m = grid.size(), n = grid[0].size();
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(visit[i][j]==false && grid[i][j]==1)
                {
                    ret = max(ret, bfs(grid, i, j));
                }
            }
        }
        return ret;
    }

    int bfs(const vector<vector<int>>& grid, int i, int j)
    {
        int count = 0;
        queue<pair<int, int>> q;
        q.emplace(i, j);
        visit[i][j] = true;
        count++;
        while(!q.empty())
        {
            auto [a, b] = q.front();
            q.pop();
            for(int k=0; k<4; k++)
            {
                int x = a+dx[k], y = b+dy[k];
                if(x>=0 && x<m && y>=0 && y<n && visit[x][y]==false && grid[x][y]==1)
                {
                    q.emplace(x, y);
                    visit[x][y] = true;
                    count++;
                }
            }
        }
        return count;
    }
};